package cn.iocoder.yudao.module.museum.controller.admin.collectionimageacquisition;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.museum.controller.admin.collectionimageacquisition.vo.CollectionImageAcquisitionPageReqVO;
import cn.iocoder.yudao.module.museum.controller.admin.collectionimageacquisition.vo.CollectionImageAcquisitionRespVO;
import cn.iocoder.yudao.module.museum.controller.admin.collectionimageacquisition.vo.CollectionImageAcquisitionSaveReqVO;
import cn.iocoder.yudao.module.museum.dal.dataobject.collectionimageacquisition.CollectionImageAcquisitionDO;
import cn.iocoder.yudao.module.museum.service.collectionimageacquisition.CollectionImageAcquisitionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 藏品二维采集信息")
@RestController
@RequestMapping("/museum/collection-image-acquisition")
@Validated
public class CollectionImageAcquisitionController {

    @Resource
    private CollectionImageAcquisitionService collectionImageAcquisitionService;

    @PostMapping("/create")
    @Operation(summary = "创建藏品二维采集信息")
    @PreAuthorize("@ss.hasPermission('museum:collection-image-acquisition:create')")
    public CommonResult<Integer> createCollectionImageAcquisition(@Valid @RequestBody CollectionImageAcquisitionSaveReqVO createReqVO) {
        return success(collectionImageAcquisitionService.createCollectionImageAcquisition(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新藏品二维采集信息")
    @PreAuthorize("@ss.hasPermission('museum:collection-image-acquisition:update')")
    public CommonResult<Boolean> updateCollectionImageAcquisition(@Valid @RequestBody CollectionImageAcquisitionSaveReqVO updateReqVO) {
        collectionImageAcquisitionService.updateCollectionImageAcquisition(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除藏品二维采集信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('museum:collection-image-acquisition:delete')")
    public CommonResult<Boolean> deleteCollectionImageAcquisition(@RequestParam("id") Long id) {
        collectionImageAcquisitionService.deleteCollectionImageAcquisition(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得藏品二维采集信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('museum:collection-image-acquisition:query')")
    public CommonResult<CollectionImageAcquisitionRespVO> getCollectionImageAcquisition(@RequestParam("id") Long id) {
        CollectionImageAcquisitionDO collectionImageAcquisition = collectionImageAcquisitionService.getCollectionImageAcquisition(id);
        return success(BeanUtils.toBean(collectionImageAcquisition, CollectionImageAcquisitionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得藏品二维采集信息分页")
    @PreAuthorize("@ss.hasPermission('museum:collection-image-acquisition:query')")
    public CommonResult<PageResult<CollectionImageAcquisitionRespVO>> getCollectionImageAcquisitionPage(@Valid CollectionImageAcquisitionPageReqVO pageReqVO) {
        PageResult<CollectionImageAcquisitionDO> pageResult = collectionImageAcquisitionService.getCollectionImageAcquisitionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CollectionImageAcquisitionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出藏品二维采集信息 Excel")
    @PreAuthorize("@ss.hasPermission('museum:collection-image-acquisition:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCollectionImageAcquisitionExcel(@Valid CollectionImageAcquisitionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CollectionImageAcquisitionDO> list = collectionImageAcquisitionService.getCollectionImageAcquisitionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "藏品二维采集信息.xls", "数据", CollectionImageAcquisitionRespVO.class,
                        BeanUtils.toBean(list, CollectionImageAcquisitionRespVO.class));
    }

}